/*================================================================================
*
*
*
*
*
* 项目名称:  210605F7LVGL
* 文件名称:  lvgl_study.c
* 创建日期:  Aug 21, 2021
* 文件描述:
*
================================================================================*/
#include "lvgl.h"
#include "lvgl_study.h"

void lv_example_style1(void)
{
  static lv_style_t style;
  lv_style_init(&style);
  lv_style_set_radius(&style, 10);

  lv_style_set_width(&style, LV_SIZE_CONTENT);
  lv_style_set_height(&style, LV_SIZE_CONTENT);

  lv_style_set_pad_hor(&style, 50);
  lv_style_set_pad_ver(&style, 50);

  lv_style_set_x(&style, lv_pct(50));
  lv_style_set_y(&style, 80);

  lv_obj_t *obj = lv_obj_create(lv_scr_act());
  lv_obj_add_style(obj, &style, 0);

  lv_obj_t *label = lv_label_create(obj);
  lv_label_set_text(label, "hello");
}

void lv_example_style2(void)
{
  static lv_style_t style;
  lv_style_init(&style);
  lv_style_set_radius(&style, 5);

  lv_style_set_bg_opa(&style, LV_OPA_COVER);
  lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_GREY, 1));
  lv_style_set_bg_grad_color(&style, lv_palette_main(LV_PALETTE_BLUE));
  lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);

  lv_style_set_bg_main_stop(&style, 0);
  lv_style_set_bg_grad_stop(&style, 160);

  lv_obj_t *obj = lv_obj_create(lv_scr_act());
  lv_obj_add_style(obj, &style, 0);
  lv_obj_center(obj);
}

/*
 * arc style
 */
void lv_example_style_7(void)
{
  static lv_style_t style;
  lv_style_init(&style);

  lv_style_set_arc_color(&style, lv_palette_main(LV_PALETTE_RED));
  lv_style_set_arc_width(&style, 4);

  lv_obj_t *obj = lv_arc_create(lv_scr_act());
  lv_obj_add_style(obj, &style, LV_PART_MAIN);
  lv_obj_center(obj);
}

/*
 * transition style
 */
void lv_example_style_10(void)
{
  static const lv_style_prop_t props[]={LV_STYLE_BG_COLOR, LV_STYLE_BORDER_COLOR, LV_STYLE_BORDER_WIDTH, 0};

  static lv_style_transition_dsc_t trans_def;
  lv_style_transition_dsc_init(&trans_def, props, lv_anim_path_linear, 100, 200, NULL);

  static lv_style_transition_dsc_t trans_pr;
  lv_style_transition_dsc_init(&trans_pr, props, lv_anim_path_linear, 500, 0, NULL);

  static lv_style_t style_def;
  lv_style_init(&style_def);
  lv_style_set_transition(&style_def, &trans_def);

  static lv_style_t style_pr;
  lv_style_init(&style_pr);
  lv_style_set_bg_color(&style_pr, lv_palette_main(LV_PALETTE_RED));
  lv_style_set_border_width(&style_pr, 6);
  lv_style_set_border_color(&style_pr, lv_palette_darken(LV_PALETTE_RED, 3));
  lv_style_set_transition(&style_pr, &trans_pr);

  lv_obj_t *obj = lv_obj_create(lv_scr_act());
  lv_obj_add_style(obj, &style_def, LV_STATE_DEFAULT);
  lv_obj_add_style(obj, &style_pr, LV_STATE_PRESSED);

  lv_obj_center(obj);
}

/*
 * add styles to parts and states
 */
void lv_example_style_13(void)
{
  static lv_style_t style_indic;
  lv_style_init(&style_indic);
  lv_style_set_bg_color(&style_indic, lv_palette_lighten(LV_PALETTE_GREEN, 3));
  lv_style_set_bg_grad_color(&style_indic, lv_palette_main(LV_PALETTE_GREEN));
  lv_style_set_bg_grad_dir(&style_indic, LV_GRAD_DIR_HOR);

  static lv_style_t style_knob_pr;
  lv_style_init(&style_knob_pr);
  lv_style_set_bg_color(&style_knob_pr, lv_palette_main(LV_PALETTE_GREEN));

  static lv_style_t style_indic_pr;
  lv_style_init(&style_indic_pr);
  lv_style_set_shadow_color(&style_indic_pr, lv_palette_darken(LV_PALETTE_GREEN, 2));
  lv_style_set_shadow_width(&style_indic_pr, 10);
  lv_style_set_shadow_spread(&style_indic_pr, 3);

  lv_obj_t *obj = lv_slider_create(lv_scr_act());
  lv_obj_add_style(obj, &style_indic, LV_PART_INDICATOR);
  lv_obj_add_style(obj, &style_knob_pr, LV_PART_KNOB|LV_STATE_PRESSED);
  lv_obj_add_style(obj, &style_indic_pr, LV_PART_INDICATOR|LV_STATE_PRESSED);
  lv_slider_set_value(obj, 70, LV_ANIM_OFF);
  lv_obj_center(obj);
}

/**************************************************************************************
 * Translate the object as they scroll
 */
static void scroll_event_cb(lv_event_t *e)
{
  lv_obj_t *cont = lv_event_get_target(e);

  lv_area_t cont_a;
  lv_obj_get_coords(cont, &cont_a);
  lv_coord_t r = lv_obj_get_height(cont)*7/10;
  lv_coord_t cont_y_center = cont_a.y1+lv_area_get_height(&cont_a)/2;

  uint32_t i;
  uint32_t child_cnt = lv_obj_get_child_cnt(cont);
  for(i=0; i<child_cnt; i++)
  {
    lv_obj_t *child = lv_obj_get_child(cont, i);
    lv_area_t child_a;
    lv_obj_get_coords(child, &child_a);

    lv_coord_t child_y_center = child_a.y1+lv_area_get_height(&child_a)/2;

    lv_coord_t diff_y = child_y_center-cont_y_center;
    diff_y = LV_ABS(diff_y);

    lv_coord_t x;

    if(diff_y>=r)
    {
      x = r;
    }
    else
    {
      uint32_t x_sqr = r*r-diff_y*diff_y;
      lv_sqrt_res_t res;
      lv_sqrt(x_sqr, &res, 0x8000);
      x =  r-res.i;
    }
    lv_obj_set_style_translate_x(child, x, LV_PART_MAIN);

    lv_opa_t opa = lv_map(x, 0, r, LV_OPA_TRANSP, LV_OPA_COVER);
    lv_obj_set_style_opa(child, LV_OPA_COVER-opa, LV_PART_MAIN);
  }
}

void lv_example_scroll_6(void)
{
  lv_obj_t *cont = lv_obj_create(lv_scr_act());
  lv_obj_set_size(cont, 200, 200);
  lv_obj_center(cont);
  lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
  lv_obj_add_event_cb(cont, scroll_event_cb, LV_EVENT_SCROLL, NULL);
  lv_obj_set_style_radius(cont, LV_RADIUS_CIRCLE, LV_PART_MAIN);
  lv_obj_set_style_clip_corner(cont, true, LV_PART_MAIN);
  lv_obj_set_scroll_dir(cont, LV_DIR_VER);
  lv_obj_set_scroll_snap_y(cont, LV_SCROLL_SNAP_CENTER);
  lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);

  uint32_t i;
  for(i=0; i<20; i++)
  {
    lv_obj_t *btn = lv_btn_create(cont);
    lv_obj_set_width(btn, lv_pct(100));

    lv_obj_t *label=lv_label_create(btn);
    lv_label_set_text_fmt(label, "Button %d", i);
  }

  lv_event_send(cont, LV_EVENT_SCROLL, NULL);
  lv_obj_scroll_to_view(lv_obj_get_child(cont, 0), LV_ANIM_OFF);
}

/********************************************************************************************
* image example
*/
void lv_example_image_00(void)
{
    lv_obj_t* cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(cont, 600, 400);
    lv_obj_center(cont);

    lv_img_t* img = lv_img_create(cont);
    LV_IMG_DECLARE(moncake);
    lv_img_set_src(img, &moncake);
    lv_obj_center(img);
}
